题解 UVA1345 【Jamie's Contact Groups】

$Description$

有$N$个人,$M$个分组,初始时每个人可能属于若干组,从每组中删除一些人,使每个人属于一组,且人数最多的组的人数最少

$Solution$

容易想到二分答案.设二分答案为$mid$,从源点$s$向$N$个人连边,容量为$1$,表示此人只能选择一组。对每个人,向他们可以属于的组连边,容量为$1$,由于流进来的流量为$1$,所以最后也只能从一条容量为$1$的边出去,即选择一个组,对每个组,向汇点$t$连边,容量为$mid$,这表示该组人数不能超过$mid$,因此,从该点流向汇点的流量即为该组人数。最后,判断最大流是否等于$N$,即判断是否每个人都有分组

$Code$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define N 400000
using namespace std;
struct node{
int to,dis,next;
}e[1070000];
inline int read(){
int x=0,w=0;char ch=getchar();
while (!isdigit(ch))w|=ch=='-',ch=getchar();
while (isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return w?-x:x;
}
int d[2000][2000],c[N],cur[N],cnt=1,head[N],inque[N],dep[N],n,m,s,t;
inline void add(int u,int v,int d){
e[++cnt].to=v;
e[cnt].dis=d;
e[cnt].next=head[u];
head[u]=cnt;
e[++cnt].to=u;
e[cnt].dis=0;
e[cnt].next=head[v];
head[v]=cnt;
}
inline bool bfs(){
memset(dep,0,sizeof(dep));
dep[s]=1;
queue<int>q;q.push(s);
while (!q.empty()){
int u=q.front();q.pop();inque[u]=0;
for (int i=head[u];i;i=e[i].next){
int v=e[i].to;
if (e[i].dis&&!dep[v]){
dep[v]=dep[u]+1;
if (!inque[v]){
q.push(v);inque[v]=1;
}
}
}
}
return dep[t];
}
int dfs(int u,int mn){
if (u==t)return mn;
int used=0,mi;
for (int &i=cur[u];i;i=e[i].next){
int v=e[i].to;
if (e[i].dis&&dep[v]==dep[u]+1)
if (mi=dfs(v,min(e[i].dis,mn-used))){
e[i].dis-=mi;
e[i^1].dis+=mi;
used+=mi;
if (used==mn)break;
}
}
return used;
}
inline int Dinic(){
int res=0;
while (bfs()){
for (int i=s;i<=t;++i)cur[i]=head[i];
res+=dfs(s,inf);
}
return res;
}
bool check(int mid){
memset(head,0,sizeof(head));cnt=1;
for (int i=1;i<=n;++i)add(s,i,1);
for (int i=1;i<=n;++i)
for (int j=1;j<=c[i];++j)
add(i,1+d[i][j]+n,1);
for (int j=1;j<=m;++j)add(j+n,t,mid);
return Dinic()==n;
}
char ss[N];
int main(){
while (1){
n=read(),m=read();s=0,t=n+m+1;
if (!n&&!m)return 0;
memset(c,0,sizeof(c));
for (int i=1;i<=n;++i){
scanf("%s",ss);
while (getchar()!='\n')
scanf("%d",&d[i][++c[i]]);
}
int l=0,r=1200,ans=0;
while (l<=r){
int mid=l+r>>1;
if (check(mid))r=mid-1,ans=mid;
else l=mid+1;
}
printf("%d\n",ans);
}
return 0;
}